﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Cashier_Interface;
using Cashier_Service;
using Cashier.utils;
using Cashier_Utils;

namespace Cashier.report
{
    public partial class MemberTopReportControl : UserControl, IDataGridViewData, IPrinter
    {
        private MainForm mainform;
        private ReportService reportService;
        private static MemberTopReportControl memberTopReportControl;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainform"></param>
        private MemberTopReportControl(MainForm mainform)
        {
            InitializeComponent();
            this.dataGridView1.ImplementsInstance = this;
            this.reportService = new ReportService();
            this.mainform = mainform;

            this.InitialComboBox();
            this.Dock = DockStyle.Fill;
        }

        /// <summary>
        /// 返回MemberTopReportControl实例
        /// </summary>
        /// <param name="mainform"></param>
        /// <returns></returns>
        public static MemberTopReportControl GetMemberTopReportControl(MainForm mainform)
        {
            if (memberTopReportControl == null)
                memberTopReportControl = new MemberTopReportControl(mainform);
            memberTopReportControl.RefreshDataGridViewData();
            return memberTopReportControl;
        }

        #region 功能方法实现

        /// <summary>
        /// 初始化ComboBox
        /// </summary>
        private void InitialComboBox()
        {
            DataTable years_dt = reportService.GetShipmentReportAllYears();
            List<DataItem> list = DataItem.GetDataItemList(years_dt, "years", String.Empty, "years");
            List<DataItem> combolist = new List<DataItem>();
            combolist.Add(DataItem.GetDataItem("", "0"));
            combolist.AddRange(list);
            comboBox_year.DisplayMember = "displayMember";
            comboBox_year.ValueMember = "valueMember";
            comboBox_year.DataSource = combolist;

            List<DataItem> month_list = new List<DataItem>();
            month_list.Add(DataItem.GetDataItem("", "0"));
            for (int i = 1; i < 13; i++)
            {
                month_list.Add(DataItem.GetDataItem(i.ToString(), i.ToString()));
            }
            comboBox_month.DisplayMember = "displayMember";
            comboBox_month.ValueMember = "valueMember";
            comboBox_month.DataSource = month_list;
        }

        #endregion

        #region IDataGridViewData 成员

        void IDataGridViewData.EnterkeyEventHandler()
        {
            return;
        }

        void IDataGridViewData.GenerateGridViewColumns(DataGridView dgv)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.Columns.Clear();
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("member_no", "编号", true, 40, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("result", "消费金额", true, 30, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("retotal", "退款金额", true, 30, ActKey.RIGHT, ActKey.RIGHT, true, false));
        }

        void IDataGridViewData.GetBindingData(int rowindex, int pageindex, int pagesize)
        {
            try
            {
                int years = Int32.Parse(comboBox_year.SelectedValue.ToString());
                int months = Int32.Parse(comboBox_month.SelectedValue.ToString());
                if (months > 0)
                {
                    if (years == 0)
                    {
                        MessageBox.Show("请选择年份！");
                        return;
                    }
                }
                DataTable dt = reportService.MemberTakeReport(1, 1000, String.Empty, years, months, 0, "member_no", "result");
                this.dataGridView1.DataSource = dt;
                if (dt != null && dt.Rows.Count > 0)
                {
                    if (rowindex > 0)
                        dataGridView1.Rows[0].Selected = false;
                    dataGridView1.Rows[rowindex].Selected = true;
                }

                mainform.SetPageInfo(dataGridView1.Recordcount, 0, 0);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        long IDataGridViewData.GetBindingDataRecord()
        {
            return 0;
        }

        #endregion


        #region 刷新数据

        /// <summary>
        /// 刷新数据
        /// </summary>
        public void RefreshDataGridViewData()
        {
            this.dataGridView1.LoadingData();
        }

        #endregion


        #region IPrinter 成员

        DataGridView IPrinter.GetAllRows()
        {
            try
            {
                ((IDataGridViewData)this).GetBindingData(0, 0, 0);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
            return this.dataGridView1;
        }

        DataGridView IPrinter.GetSelectedRows()
        {
            return this.dataGridView1;
        }

        void IPrinter.VisibleColumn(string columnname)
        {
            try
            {
                if (this.dataGridView1.Columns[columnname].Visible)
                    this.dataGridView1.Columns[columnname].Visible = false;
                else
                    this.dataGridView1.Columns[columnname].Visible = true;
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        #endregion

        #region 控件事件

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_query_Click(object sender, EventArgs e)
        {
            this.RefreshDataGridViewData();
        }

        /// <summary>
        /// 格式化单元格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.Value != null)
            {
                String typeName = e.Value.GetType().Name;
                if (typeName.Equals("Decimal"))
                    e.Value = ((decimal)e.Value).ToString("f2");
                else if (typeName.Equals("Double"))
                    e.Value = ((double)e.Value).ToString("f2");
            }
        }

        #endregion

        #region 方法重写

        /// <summary>
        /// 重写键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            if (keyData == Keys.F8)
            {
                this.RefreshDataGridViewData();
            }
            return b;
        }

        #endregion
    }
}
